using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Generators;
using AvaloniaApplication1.Extensions;
using AvaloniaApplication1.ViewModels;

namespace AvaloniaApplication1;

public class TransitioningContentControlAssists
{
    public static readonly AttachedProperty<ApplicationPage> CurrentPageProperty =
        AvaloniaProperty
            .RegisterAttached<TransitioningContentControlAssists, TransitioningContentControl, ApplicationPage>(
                "CurrentPage", coerce: OnCallback);

    public static void SetCurrentPage(TransitioningContentControl obj, ApplicationPage value) =>
        obj.SetValue(CurrentPageProperty, value);

    public static ApplicationPage GetCurrentPage(TransitioningContentControl obj) => obj.GetValue(CurrentPageProperty);

    private static ApplicationPage OnCallback(AvaloniaObject sender, ApplicationPage page)
    {
        if (sender is not TransitioningContentControl transitioningContentControl)
            return page;

        transitioningContentControl.Content = page.ToBasePage();

        return page;
    }
}